<?php

/**
 * 权限管理
 * @author 吴昌勇
 *
 */
class PermisionController{
    
    public function loadAllRole(){
        $sql = "select * from tb_role";
        $dbutil = new DBUtil();
        echo json_encode($dbutil->query($sql));
    }
    
    /**
     * 查询用户角色关联关系
     */
    public function loadAllUserRole(){
        $rid = $_GET["rid"];
        $sql = "select u.uid,u.userName,(select 1 from tb_userrole ur where ur.uid=u.uid and ur.rid=?) as isChecked from tb_user u";
        $dbutil = new DBUtil();
        echo json_encode($dbutil->query($sql, array($rid)));
    }
    
    /**
     * 修改用户角色的关联关系
     */
    public function editUserRole(){
        $rid = $_GET["rid"];
        $uids = $_GET["uids"];
        $dbutil = new DBUtil();
        $pdo = $dbutil->getPdo();
        try{
            //开启事务
            $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
            $pdo->beginTransaction();
            $sql = "delete from tb_userrole where rid=?";
            $ps = $pdo->prepare($sql);
            $ps->execute(array($rid));
            $sql = "insert into tb_userrole(uid,rid) values(?,?)";
            $ps = $pdo->prepare($sql);
            foreach ($uids as $uid){
                $ps->execute(array($uid, $rid));
            }
            $pdo->commit();//提交事务
        }catch (Exception $e){
            echo $e;
            $pdo->rollback();//事务回滚
        }
        echo "{\"result\":\"ok\"}";
    }
    
    //select pid,pname,(select 1 from tb_rolepermision rp where rp.pid=p.pid and rp.rid=1) as isChecked from tb_permision p
}

?>